<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 钉钉机器人FeedCard消息 v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace ding\msg\robot;

/**
 * 给机器人发送的FeedCard消息
 *
 * @package ding\msg\robot
 */
class FeedCardRobotMsg extends RobotMsg {
    protected $links = [];

    /**
     * FeedCard消息构造器
     */
    public function __construct() {
        parent::__construct("feedCard");
    }

    /**
     * 添加图文链接
     *
     * @param $title
     * @param $messageURL
     * @param $picURL
     * @return $this
     */
    public function addLink($title, $messageURL, $picURL) {
        $this->links[] = [
            'title' => $title,
            'messageURL' => $messageURL,
            'picURL' => $picURL
        ];
        return $this;
    }

    /**
     * 转换为可发送的消息
     * {
     *     "feedCard": {
     *         "links": [
     *             {
     *                 "title": "时代的火车向前开",
     *                 "messageURL": "https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI",
     *                 "picURL": "https://www.dingtalk.com/"
     *             },
     *             {
     *                 "title": "时代的火车向前开2",
     *                 "messageURL": "https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI",
     *                 "picURL": "https://www.dingtalk.com/"
     *             }
     *         ]
     *     },
     *     "msgtype": "feedCard"
     * }
     * @return array
     */
    public function toMsg() {
        return [
            'msgtype' => $this->msgtype,
            'feedCard' => [
                'links' => $this->links
            ]
        ];
    }

}